package mebp.genetic.stop;

import mebp.genetic.Individuum;
import mebp.genetic.Population;

import org.apache.log4j.Logger;

public class StarveStrategy implements StopStrategy {

	Logger log = Logger.getLogger(StarveStrategy.class);
	Individuum best=null;
	int counter;
	int rounds;
	int countGenerations;
	
	public StarveStrategy(int howManyRoundsWithoutImprovement) {
		counter=0;
		rounds=howManyRoundsWithoutImprovement;
	}
	
	
	@Override
	public boolean isFinished(Population population) {
		if (best==null) best=population.getBestResult();
		if (best.getFitness()>population.getBestResult().getFitness()) {
			best=population.getBestResult();
			counter=rounds;
			log.debug("Starve: found new best at g# "+countGenerations+" with fit="+best.getFitness());
		} else {
			counter--;
		}
		countGenerations++;
		return counter<0;
	}

	@Override
	public void start() {
		best=null;
		counter=rounds;
		countGenerations=0;
	}

	@Override
	public Individuum getBest() {
		return best;
	}
	
	
	

}
